/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yinsh
 * Date: 2025-02-20
 * Time: 21:11
 */
public class Test {
    public void qSort (int[] array) {
        qSortFunc(array,0,array.length-1);
    }


    private void qSortFunc (int[] array,int left,int right) {
        int pivot = partition(array,left,right);
        if (left >= right ) return;
        while (left < right) {
            qSortFunc(array, left, pivot);
            left = pivot + 1;
            pivot = right;
        }
    }



    private int partition (int[] array,int left,int right) {
        //三数取一
        int mid = (left+right)/2;
        int start = left;
        if (array[left] > array[right]) {
            swap(array,left,right);
        }
        if (array[left] > array[mid]) {
            swap(array,left,mid);
        }
        if (array[mid] > array[right]) {
            swap(array,mid,right);
        }
        int key = array[mid];

    while (left < right) {
        while (left < right && array[right] >= key) {
            right--;
        }
        while (left < right && array[left] <= key) {
            left++;
        }
        swap(array, left, right);
    }
        swap (array,start,right);
        return left;
    }

    private void swap(int[] array,int x,int y) {
        int tamp = array[x];
        array[x] = array[y];
        array[y] = tamp;
    }
}
